package model

import (
	"stock/global"

	"gorm.io/gorm"
)

type ColumnDetailModel struct {
	Base
}

type SysColumnDetail struct {
	Id           uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	Module       string `json:"module" gorm:"column:module;"`
	CategoryId   uint   `json:"category_id" gorm:"column:category_id;"`
	MerchantType uint8  `json:"merchant_type" gorm:"<-:false; column:merchant_type;"`
	MerchantId   uint   `json:"merchant_id" gorm:"<-:false; column:merchant_id;"`
	ValueId      uint   `json:"value_id" gorm:"column:value_id;"`
	Status       uint8  `json:"status" gorm:"column:status;"`
	Sort         uint   `json:"sort" gorm:"column:sort;"`
	CreateAt     string `json:"create_at" gorm:"<-:false; column:create_at;"`
	UpdatedAt    string `json:"updated_at" gorm:"column:updated_at;"`
}

type RequestColumnDetail struct {
	MerchantType uint8
	MerchantId   uint
	Model        string
	CategoryId   []uint
	Status       uint8
}

func (m *ColumnDetailModel) TabName() string {
	return global.CONF.Mysql.Prefix + "sys_column_detail"
}

func (m *ColumnDetailModel) List(param RequestColumnDetail, page global.PageSort) ([]SysColumnDetail, error) {

	var where SysColumnDetail
	if param.MerchantType != 0 {
		where.MerchantType = param.MerchantType
	}
	if param.MerchantId != 0 {
		where.MerchantId = param.MerchantId
	}
	if param.Model != "" {
		where.Module = param.Model
	}
	where.Status = param.Status
	var column []SysColumnDetail
	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.CategoryId) > 0 {
		tx = tx.Where("category_id IN ?", param.CategoryId)
	}
	err := tx.Order(page.Sortfield + " " + page.Sort).Offset(page.Page).Limit(page.Limit).Find(&column)
	if err.Error != nil {
		return column, err.Error
	}
	return column, err.Error
}
